/**
 * @ Author 何涛
 * @ Date 2016/03/12 02:10:06
 */
gpNamespace.GameUserInfoLayer = cc.Layer.extend({
	player: null,

	tv: null,
//	tvTouchable: false,

	sexBackground: null,
	rate: null,
	gameHistory: null,

	ctor: function (player) {
		this._super();

		this.player = player;
	},

	onEnter: function () {
		this._super();
		//背景
		var background = new cc.Sprite(gpNamespace.xinzeng.inneruserinfo_bg);
		background.attr({
			x: 640,
			y: 360,
			anchorX: 0.5,
			anchorY: 0.5
		});
		this.addChild(background);

		//头像男女背景
		var sexBackground = new cc.Sprite(gpNamespace.dj.Headbackground);
		sexBackground.attr({
			x: 36,
			y: 200,
			anchorX: 0,
			anchorY: 0
		});
		background.addChild(sexBackground);
		this.sexBackground = sexBackground;

		//头像
		var userHeadImgUrl = this.player.userItem.headImgUrl;
		if (userHeadImgUrl == "") {
			var img = new LoadHead(this.player.userItem.headId);
			img.loadImg();
			var x = (sexBackground.getContentSize().width) / img.getContentSize().width;
			var y = (sexBackground.getContentSize().height) / img.getContentSize().height;
			img.setScale(x, y);
			img.attr({
				x: sexBackground.getContentSize().width / 2 - 1,
				y: sexBackground.getContentSize().height / 2 + 2
			});
			sexBackground.addChild(img);
		}
		else {
			tyNamespace.loadImgByUrl(userHeadImgUrl, this.addUserHead, this);
		}

		//vip
		var isVip = this.player.userItem.isVip;
		if (isVip) {
			var headBackground = new cc.Sprite(gpNamespace.dj.VIP);
			headBackground.attr({
				x: 10,
				y: sexBackground.getContentSize().width + 10,
				anchorX: 0,
				anchorY: 1
			});
			sexBackground.addChild(headBackground);
		}

		//nickName
		var nickNameLabel = new cc.LabelTTF(this.player.userItem.nickName, "Arial", 25);
		nickNameLabel.setFontFillColor(cc.color.WHITE);
		nickNameLabel.attr({
			x: 300,
			y: 380,
			anchorX: 0,
			anchorY: 0
		});
		background.addChild(nickNameLabel);

		//胜率
		var rate = new cc.LabelTTF("胜率：", "Arial", 25);
		rate.setFontFillColor(cc.color.WHITE);
		rate.attr({
			x: 300,
			y: 330,
			anchorX: 0,
			anchorY: 0
		});
		background.addChild(rate);
		this.rate = rate;

		//战绩
		var gameHistory = new cc.LabelTTF("战绩：", "Arial", 25);
		gameHistory.setFontFillColor(cc.color.WHITE);
		gameHistory.attr({
			x: 300,
			y: 280,
			anchorX: 0,
			anchorY: 0
		});
		background.addChild(gameHistory);
		this.gameHistory = gameHistory;

//		//金币背景
//var goldBackground = new cc.Sprite(gpNamespace.innerUserInfo.GoldBackground);
//		goldBackground.attr({
//		x: 238,
//		y: 160,
//		anchorX: 0,
//		anchorY: 0
//		});
//		background.addChild(goldBackground);

		//金币
//		var gold = this.player.userItem.lScore;
//		for (var i = 0; i < gold.toString().length; i++) {
//		var str = tyNamespace.FullZero(gold.toString().substr(i, 1), 3);
//		var spritName = "#num_top_" + str + "u.png";
//		var countCell = new cc.Sprite(spritName);
//		countCell.attr({
//		x: (125 - gold.toString().length * 12 / 2) + 12 * i,
//		y: 30
//		});
//		goldBackground.addChild(countCell);
//		}

		var gold = new tyNamespace.NumLayer(this.player.userItem.lScore, 19, 0, "sz_cdd_js_");
		gold.attr({
			x: 400,
			y: 232,
		});
		background.addChild(gold, 2);	

		//举报
//		var jubaoItem = new cc.MenuItemImage(gpNamespace.innerUserInfo.Jubao, gpNamespace.innerUserInfo.Jubao, this.jubaoCallbackFunction, this);
//		jubaoItem.attr({
//		x: 148 / 2,
//		y: 53 / 2
//		});
//		var jubaoMenu = new cc.Menu(jubaoItem);
//		jubaoMenu.attr({
//		x: 410,
//		y: 310
//		});
//		background.addChild(jubaoMenu);
//		jubaoMenu.setVisible(false);

//		//我的道具
//		var propItem = new cc.MenuItemImage(gpNamespace.innerUserInfo.Prop, gpNamespace.innerUserInfo.Prop, this.propCallbackFunction, this);
//		propItem.attr({
//		x: 148 / 2,
//		y: 53 / 2
//		});
//		var propMenu = new cc.Menu(propItem);
//		propMenu.attr({
//		x: 410,
//		y: 310
//		});
//		background.addChild(propMenu);
//		propMenu.setVisible(false);

//		if (this.player.orderIndex == 0) {
//		jubaoMenu.setVisible(false);
//		propMenu.setVisible(true);
//		} else {
//		jubaoMenu.setVisible(true);
//		propMenu.setVisible(false);
//		}

		//道具背景
//		var djBackground = new cc.Sprite(gpNamespace.innerUserInfo.DjBackground);
//		djBackground.attr({
//		x: 33,
//		y: 24,
//		anchorX: 0,
//		anchorY: 0
//		});
//		background.addChild(djBackground);

		//滚轮控件
		var tv = new tableViewPro.TableView(this, cc.size(670, 150), 0);
		tv.setPosition(75, 35);
		tv.setAnchorPoint(0, 0);
		background.addChild(tv);
		this.tv = tv;
//		tv.setTouchEnabled(false);

		gpNamespace.TyNetManager.getInstance().setInnerUserInfoLayer(this);

		tyNamespace.addWaitLayer();
		xinyou.netManager.getInnerUserInfo(this.player.orderIndex);
	},

	addUserHead: function (texture) {
		var imgSprite;
		if (!texture) {
			var robotIDStr = cc.sys.localStorage.getItem("myHeadId");

			if (robotIDStr == null || robotIDStr.length == 0) {
				robotIDStr = Math.round(Math.random() * (800 - 1)) + 1;
				cc.sys.localStorage.setItem("myHeadId", robotIDStr);
			}

			cc.log("robotIDStr:" + robotIDStr);

			imgSprite = new cc.Sprite("#zjm_qqtx_" + robotIDStr + "u.png");
			imgSprite.setScale(0.8 * 2.5, 0.75 * 2.5);
		}
		else {
			imgSprite = new cc.Sprite();
			imgSprite.initWithTexture(texture);

			var size = imgSprite.getContentSize();
			if (size.width != 100 || size.height != 100) {
				var scaleX = 100 / size.width;
				var scaleY = 100 / size.height;
			}
			imgSprite.setScale(scaleX, scaleY);
		}

		var x = 187 / imgSprite.getContentSize().width;
		var y = 187 / imgSprite.getContentSize().height;
		imgSprite.setScale(x, y);
		imgSprite.attr({
			x: 17,
			y: 33,
			anchorX:0,
			anchorY:0
		});
		this.sexBackground.addChild(imgSprite);
	},

	jubaoCallbackFunction: function (sender) {
		cc.log("举报");

		tyNamespace.setMenuEnable(sender);

		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		xinyou.netManager.feedback(4, this.player.orderIndex);

		this.getParent().getParent().removeFromParent(true);
	},

	propCallbackFunction: function (sender) {
		cc.log("我的道具");

		tyNamespace.setMenuEnable(sender);

		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer();
		modelLayer.setTouchArea(cc.size(1040, 560), cc.p(120, 80));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 100);

		var gamePropLayer = new gpNamespace.GamePropLayer();
		gamePropLayer.attr({
			x: 0,
			y: cc.winSize.height
		});
		modelLayer.addChild(gamePropLayer);

		tyNamespace.AnimationManager.nodeJumpShow(gamePropLayer, -cc.winSize.height);
	},

	setWinRate: function (content) {
		var str = "胜率：" + content;
		this.rate.setString(str);
	},

	setWinLose: function (content) {
		var str = "战绩：" + content;
		this.gameHistory.setString(str);
	},

	setGameUserInfo: function () {
		//cc.log(gpNamespace.GameUserInfo.RESULT);
		//cc.log(gpNamespace.GameUserInfo.LOSECOUNT);
		//cc.log(gpNamespace.GameUserInfo.DRAWCOUNT);
		//cc.log(gpNamespace.GameUserInfo.WINCOUNT);
		//cc.log(gpNamespace.GameUserInfo.ITEMCOUNT);
		//for (var a = 0; a < gpNamespace.GameUserInfo.ITEMCOUNT; a++) {
		//    cc.log(gpNamespace.GameUserInfo.ITEMS[a][0]);
		//    cc.log(gpNamespace.GameUserInfo.ITEMS[a][1]);
		//    cc.log(gpNamespace.GameUserInfo.ITEMS[a][2]);
		//    cc.log(gpNamespace.GameUserInfo.ITEMS[a][3]);
		//    cc.log(gpNamespace.GameUserInfo.ITEMS[a][4]);
		//}
		
		var str = gpNamespace.GameUserInfo.WINCOUNT + "胜" + gpNamespace.GameUserInfo.LOSECOUNT + "负";
		var rate = (parseInt(gpNamespace.GameUserInfo.WINCOUNT) / (parseInt(gpNamespace.GameUserInfo.WINCOUNT) + parseInt(gpNamespace.GameUserInfo.LOSECOUNT)) * 100).toFixed(2) + "%";
		if (parseInt(gpNamespace.GameUserInfo.WINCOUNT) + parseInt(gpNamespace.GameUserInfo.LOSECOUNT) == 0)
			rate = "0%";
		this.setWinRate(rate);
		this.setWinLose(str);

//		this.tv.setDataSource(this);
//		this.tv.setDelegate(this);
		this.tv.clearData();
		this.tv.addData({
			"barInfo": {"width": 0, "height": 0},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 4},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": gpNamespace.GameUserInfo.ITEMCOUNT,
			            		 "imageName": "",
			            		 "height": 150
			            	 }
			             }
			             ]
		});
		this.tv.showData();
		this.tv.setTouchEnabled(true);
//		this.tvTouchable = true;

		//cells上加表现
		var cellCount = this.tv.currentItem.cellInfo.cellCount;
		var cellType = this.tv.currentItem.cellInfo.cellType;
		var currentPage = this.tv.currentPage;
		for (var a = 0; a < cellCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
			cell = currentPage.getChildByTag(a);
			break;
//			case 1://menu
//			cell = currentPage.getChildByName(gpNamespace.TVMENU).getChildByTag(a);
//			break;
			}
			this.setTVCell(a, cell);
		}
	},

	setTVCell: function (index, cell) {
//		cc.spriteFrameCache.addSpriteFrames(gpNamespace.djRes.Dj_plist);
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkgameTweenRes.game_tween_plist);
		var goodIcon;
		if (gpNamespace.GameUserInfo.ITEMS[index][1] >= 4000 && gpNamespace.GameUserInfo.ITEMS[index][1] < 4017) {
			//道具图标
			var goodIconID = gpNamespace.GameUserInfo.ITEMS[index][1];
			var str = tyNamespace.FullZero((goodIconID - 4006).toString(), 3);
			var goodIconName = "#cdd_xtk_wjxx_" + str + "u.png";//"#icon_prop_" + str + "u.png";

//			goodIcon = new cc.Sprite(goodIconName);
			goodIcon = new cc.MenuItemImage(goodIconName,goodIconName,this.cellCallback,this);
			goodIcon.attr({
				x: 0,
				y: 0,
				anchorX: 0,
				anchorY: 0
			});
			goodIcon.setUserData(index);
			var menu = new  cc.Menu(goodIcon);
			menu.attr({
				x: 0,
				y: 0,
				anchorX: 0,
				anchorY: 0
			});
			cell.addChild(menu);
//			var x = cell.getContentSize().width / goodIcon.getContentSize().width;
//var y = cell.getContentSize().height / goodIcon.getContentSize().height;
//			goodIcon.setScale(x, y);
//			goodIcon.setTag(1);
		}

		//道具背景
//		var itemBack = new cc.Sprite();
//		itemBack.attr({
//		x: 0,
//		y: 0,
//		anchorX: 0,
//		anchorY: 0
//		});
//		cell.addChild(itemBack);
//		itemBack.setTag(2);

		//数量
		var count = gpNamespace.GameUserInfo.ITEMS[index][2];

		var countNode = new tyNamespace.NumLayer(count, 19, 0, "sz_cdd_js_");
		countNode.attr({
			x: 60,
			y: 15
		});
		goodIcon.addChild(countNode, 2);	

//		for (var i = 0; i < count.toString().length; i++) {
//		var str = parseInt(count.toString().substr(i, 1)) + 80;
//		var spritName = "#ddz_sz_0" + str + "u.png";
//		var countCell = new cc.Sprite(spritName);
//		countCell.attr({
//		x: (45 - count.toString().length / 2 * 12) + 12 * i,
//		y: 15
//		});
//		itemBack.addChild(countCell);
//		}

		//金币背景
		cc.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>"+gpNamespace.dj.DjGoldBg);
		var goldBack = new cc.Sprite(gpNamespace.dj.DjGoldBg);
		goldBack.attr({
			x: 12,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		cell.addChild(goldBack);
		goldBack.setTag(3);
		goldBack.setVisible(false);

	//		//价格
		var price = gpNamespace.GameUserInfo.ITEMS[index][3];
		var priceNode = new tyNamespace.NumLayer(price, 19, 0, "sz_cdd_js_");
		priceNode.attr({
			x: 60,
			y: 15,
			anchorX:0,
			anchorY:0.5
		});
		goldBack.addChild(priceNode, 2);	
		
		//for (var i = 0; i < price.toString().length; i++) {
		//var str = parseInt(price.toString().substr(i, 1)) + 80;
		//var spritName = "#ddz_sz_0" + str + "u.png";
		//var countCell = new cc.Sprite(spritName);
		//countCell.attr({
		//x: (64 - price.toString().length / 2 * 12) + 12 * i,
		//y: 15
		//});
		//goldBack.addChild(countCell);
		//}
		
		if (count == 0) {
			countNode.setVisible(false);
			goldBack.setVisible(true);
		}
		else {
			countNode.setVisible(true);
			goldBack.setVisible(false);
		}
		//
		////按钮
		//var item = new cc.MenuItemImage(gpNamespace.innerUserInfo.ItemModel, gpNamespace.innerUserInfo.ItemModel, this.itemCallbackFunction, this);
		//item.attr({
		//    x: 90 / 2,
		//    y: 89 / 2
		//});
		//item.setUserData([gpNamespace.GameUserInfo.ITEMS[index][0], this.player.orderIndex]);
		//var menu = new tableViewPro.Menu(item);
		//menu.attr({
		//    x: 0,
		//    y: 0
		//});
		//cell.addChild(menu);
		//menu.setMyParent(this.tv);
		//menu.setTouchArea(this.tv.clippingNode.getContentSize(), this.tv.clippingNode.getParent().convertToWorldSpace(this.tv.clippingNode.getPosition()));
		//menu.setTag(4);
	},

	getCellData:function(){
		return gpNamespace.GameUserInfo.ITEMS;
	},

	cellCallback:function(sender){
		cc.log("使用桌面道具");

		tyNamespace.setMenuEnable(sender);

//		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		var index = sender.getUserData();
		var data = gpNamespace.GameUserInfo.ITEMS;
		var goodID = data[index][0];

		tyNamespace.addWaitLayer();
		xinyou.netManager.useProp(goodID, this.player.orderIndex);
	},

	itemCallbackFunction: function (sender) {
		cc.log("使用桌面道具");

		tyNamespace.setMenuEnable(sender);

		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		var data = sender.getUserData();

		var goodID = data[0];
		var index = data[1];

		tyNamespace.addWaitLayer();
		xinyou.netManager.useProp(goodID, index);
	}
});